import 'package:flutter/material.dart';
import 'package:flutterapp/models/general_type.dart';
import 'package:flutterapp/page/home/common_title/index.dart';
import 'package:flutterapp/page/home/tab_search/filter_bar/data.dart';
import 'package:flutterapp/page/utils/scopoed_model_helper.dart';
import 'package:flutterapp/scoped_model/room_filter.dart';

class FilterDrawer extends StatelessWidget {
  const FilterDrawer({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var dataList = ScopedModelHelper.getModel<FilterBarModel>(context).dataList;
    roomTypeList = dataList['roomTypeList'];
    orientedList = dataList['orientedList'];
    floorList = dataList['floorList'];
    print('--------------------');
    print(orientedList);
    print(roomTypeList);
    var selectedIds = ScopedModelHelper.getModel<FilterBarModel>(context)
        .selectedList
        .toList();

    _onChange(String val) {
      ScopedModelHelper.getModel<FilterBarModel>(context)
          .setSelectedListToggleItem(val);
    }

    return Drawer(
      child: SafeArea(
        child: ListView(
          children: [
            CommonTitle(
              '户型',
            ),
            CommonTypeList(
              dataList: roomTypeList,
              selectedIds: selectedIds,
              onChange: _onChange,
            ),
            CommonTitle(
              '朝向',
            ),
            CommonTypeList(
              dataList: orientedList,
              selectedIds: selectedIds,
              onChange: _onChange,
            ),
            CommonTitle(
              '楼层',
            ),
            CommonTypeList(
              dataList: floorList,
              selectedIds: selectedIds,
              onChange: _onChange,
            ),
          ],
        ),
      ),
    );
  }
}

class CommonTypeList extends StatelessWidget {
  final List<GeneralType> dataList;
  final List<String> selectedIds;
  final ValueChanged<String> onChange;

  const CommonTypeList(
      {Key key, this.dataList, this.selectedIds, this.onChange})
      : super(key: key);

//右侧的筛选抽屉
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 10.0, right: 10.0),
      child: Wrap(
        spacing: 10.0,
        runSpacing: 10.0,
        children: dataList.map((e) {
          var isActive = selectedIds.contains(e.id);
          return GestureDetector(
            onTap: () {
              //时间不为空 将返回id
              if (onChange != null) onChange(e.id);

              //selectedIds.add(e.id);
              print(selectedIds.toString());
            },
            //空白区域点击
            behavior: HitTestBehavior.translucent,
            child: Container(
              width: 100.0,
              height: 40.0,
              decoration: BoxDecoration(
                border: Border.all(width: 1.0, color: Colors.green),
                color: isActive ? Colors.green : Colors.white,
              ),
              child: Center(
                child: Text(e.name,
                    style: TextStyle(
                        color: isActive ? Colors.white : Colors.green)),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }
}
